package com.ejd.web.vo.goods.bag;

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.hibernate.SessionFactory;

import com.ejd.hibernate.dao.support.Page;
import com.ejd.service.IApplyService;
import com.ejd.service.IBasemetalService;
import com.ejd.service.IBrandService;
import com.ejd.service.IColorService;
import com.ejd.service.IGoodsService;
import com.ejd.service.IMaterialService;
import com.ejd.service.IShoesClassService;
import com.ejd.service.IShoesStyleService;
import com.ejd.service.ISubCategoryService;
import com.ejd.utils.CommonUtil;
import com.ejd.utils.DESPlusUtil;
import com.ejd.utils.MessageFactory;
import com.ejd.utils.PageExistUtil;
import com.ejd.utils.SpringFacesUtil;
import com.ejd.web.bo.impl.Apply;
import com.ejd.web.bo.impl.Basemetal;
import com.ejd.web.bo.impl.Brand;
import com.ejd.web.bo.impl.Color;
import com.ejd.web.bo.impl.Goods;
import com.ejd.web.bo.impl.Material;
import com.ejd.web.bo.impl.SubCategory;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.SpringBeanConstants;

public class ListBagPageBean {
	private String defaultHql;
	private boolean propertyOpenFlag = true;//this is for open or close the property windows
	private boolean searchParamChangeFlag = false;
	private String[] applys = null;
	private SelectItem[] applyItems = null;
	private String[] brandCodes = null;
	private SelectItem[] brandCodeItems = null;
	private String[] secondCategories = null;
	private SelectItem[] secondCategoryItems = null;
	private String[] materials = null;
	private SelectItem[] materialItems = null;
	private String[] colors = null;
	private SelectItem[] colorItems = null;

	private Page<Goods,Long> page = new Page<Goods,Long>();
	private SelectItem[] pageItems = null;
	private Integer pageSize =30;
	private Integer pageNo = 1;
	
	private static SelectItem[] pageSizeItems = {
		new SelectItem(new Long(30),"30"),
		new SelectItem(new Long(60),"60"),
		new SelectItem(new Long(100),"100"),
	};
	private String priceBetween = "N";
	private static SelectItem[] priceBetweenItems = {
		new SelectItem("N","显示所有"),
		new SelectItem("A","200以下"),
		new SelectItem("B","200-300"),
		new SelectItem("C","300-400"),
		new SelectItem("D","400-500"),
		new SelectItem("E","500以上"),
	};
	private String sortStyle = "N";//default is viewint asc
	private static SelectItem[] sortStyleItems = {
		new SelectItem("NO","默认排序"),
		new SelectItem("PA","价格从低到高"),
		new SelectItem("PD","价格从高到低"),
		new SelectItem("TA","上市由早到晚"),
		new SelectItem("TD","上市由晚到早"),
	};
	
	public String getDefaultHql() {
		return defaultHql;
	}

	public void setDefaultHql(String defaultHql) {
		this.defaultHql = defaultHql;
	}
	// this if for search data
	private String hql;
	private Object[] values;// search parameter
	// end
	
	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public SelectItem[] getPageSizeItems() {
		return pageSizeItems;
	}

	public void setPageSizeItems(SelectItem[] pageSizeItems) {
		ListBagPageBean.pageSizeItems = pageSizeItems;
	}
	public boolean isPropertyOpenFlag() {
		return propertyOpenFlag;
	}


	public void setPropertyOpenFlag(boolean propertyOpenFlag) {
		this.propertyOpenFlag = propertyOpenFlag;
	}


	public boolean isSearchParamChangeFlag() {
		return searchParamChangeFlag;
	}

	public void setSearchParamChangeFlag(boolean searchParamChangeFlag) {
		this.searchParamChangeFlag = searchParamChangeFlag;
	}

	public String[] getApplys() {
		return applys;
	}

	public void setApplys(String[] applys) {
		this.applys = applys;
	}

	public SelectItem[] getApplyItems() {
		return applyItems;
	}

	public void setApplyItems(SelectItem[] applyItems) {
		this.applyItems = applyItems;
	}
	public Integer getApplyItemsLength(){
		return this.getApplyItems().length;
	}
	public String[] getBrandCodes() {
		return brandCodes;
	}


	public void setBrandCodes(String[] brandCodes) {
		this.brandCodes = brandCodes;
	}

	public SelectItem[] getBrandCodeItems() {
		return brandCodeItems;
	}

	public void setBrandCodeItems(SelectItem[] brandCodeItems) {
		this.brandCodeItems = brandCodeItems;
	}

	public Integer getBrandCodeItemsLength(){
		return this.getBrandCodeItems().length;
	}

	public String[] getSecondCategories() {
		return secondCategories;
	}


	public void setSecondCategories(String[] secondCategories) {
		this.secondCategories = secondCategories;
	}



	public SelectItem[] getSecondCategoryItems() {
		return secondCategoryItems;
	}


	public void setSecondCategoryItems(SelectItem[] secondCategoryItems) {
		this.secondCategoryItems = secondCategoryItems;
	}

	public Integer getSecondCategoryItemsLength(){
		return this.getSecondCategoryItems().length;
	}

	public String[] getMaterials() {
		return materials;
	}


	public void setMaterials(String[] materials) {
		this.materials = materials;
	}


	public SelectItem[] getMaterialItems() {
		return materialItems;
	}


	public void setMaterialItems(SelectItem[] materialItems) {
		this.materialItems = materialItems;
	}


	public Integer getMaterialItemsLength(){
		return this.getMaterialItems().length;
	}


	public String[] getColors() {
		return colors;
	}

	public void setColors(String[] colors) {
		this.colors = colors;
	}

	public SelectItem[] getColorItems() {
		return colorItems;
	}

	public void setColorItems(SelectItem[] colorItems) {
		this.colorItems = colorItems;
	}

	public Integer getColorItemsLength(){
		return this.getColorItems().length;
	}
	
	public Page<Goods, Long> getPage() {
		return page;
	}


	public void setPage(Page<Goods, Long> page) {
		this.page = page;
	}


	public SelectItem[] getPageItems() {
		return pageItems;
	}

	public void setPageItems(SelectItem[] pageItems) {
		this.pageItems = pageItems;
	}

	public String getHql() {
		return hql;
	}

	public void setHql(String hql) {
		this.hql = hql;
	}

	public Object[] getValues() {
		return values;
	}

	public void setValues(Object[] values) {
		this.values = values;
	}

	public String getPriceBetween() {
		return priceBetween;
	}

	public void setPriceBetween(String priceBetween) {
		this.priceBetween = priceBetween;
	}

	public SelectItem[] getPriceBetweenItems() {
		return priceBetweenItems;
	}

	public void setPriceBetweenItems(SelectItem[] priceBetweenItems) {
		ListBagPageBean.priceBetweenItems = priceBetweenItems;
	}

	public String getSortStyle() {
		return sortStyle;
	}

	public void setSortStyle(String sortStyle) {
		this.sortStyle = sortStyle;
	}

	public SelectItem[] getSortStyleItems() {
		return sortStyleItems;
	}

	public void setSortStyleItems(SelectItem[] sortStyleItems) {
		ListBagPageBean.sortStyleItems = sortStyleItems;
	}

	public ListBagPageBean(){
		
	}

	public String openPropertyWindows() {
		this.setPropertyOpenFlag(true);
		return null;
	}
	public String closePropertyWindows() {
		this.setPropertyOpenFlag(false);
		return null;
	}
	
	protected Page<Goods,Long> getData(String hql, int pageNo, int pageSize, Object... values) {
		IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
		goodsService.setEntityClass(Goods.class);
		goodsService.setPKClass(Long.class);
		Page<Goods,Long> resultPage = goodsService.pagedQuery(hql, pageNo, pageSize,values); 
		return resultPage;
	}
	protected SelectItem[] initApply() {
		IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
		goodsService.setEntityClass(Goods.class);
		goodsService.setPKClass(Long.class);
		List<String> params = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		generatePropertyParamsAndValues(EjdConstants.APPLY,params, values);
		// start set base apply items
		List<String> theApplys = goodsService.getPropertyCodes(EjdConstants.APPLY,params, values);
		SelectItem[] tempApplyItems = null;
		if (null != theApplys && !(theApplys.isEmpty())) {
			IApplyService<Apply,Long> applyService = (IApplyService<Apply,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.APPLY_SERVICE_BEAN_NAME);
			applyService.setEntityClass(Apply.class);
			applyService.setPKClass(Long.class);
			tempApplyItems = new SelectItem[theApplys.size()];
			for (int i=0; i< theApplys.size(); i ++) {
				String sapply = theApplys.get(i);
				Apply apply = applyService.findUniqueBy("applyCode", sapply);
				String applyNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,apply.getApplyName(), FacesMessage.SEVERITY_ERROR).getDetail();
				tempApplyItems[i] = new SelectItem(apply.getApplyCode(),applyNameN18);
			}
		} else {
			tempApplyItems = new SelectItem[1];
			tempApplyItems[0] = new SelectItem("","");
		}
		return tempApplyItems;
		// end set base apply items
	}
	protected SelectItem[] initBrandCodes(){
		// start set brand code items
		IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
		goodsService.setEntityClass(Goods.class);
		goodsService.setPKClass(Long.class);
		List<String> params = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		generatePropertyParamsAndValues(EjdConstants.BRAND_CODE, params, values);
		List<String> theBrandCodes = goodsService.getPropertyCodes(EjdConstants.BRAND_CODE,params,values);
		SelectItem[] tempBrandItems = null;
		if (null != theBrandCodes && !(theBrandCodes.isEmpty())) {
			IBrandService<Brand,Long> brandService = (IBrandService<Brand,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.BRAND_SERVICE_BEAN_NAME);
			brandService.setEntityClass(Brand.class);
			brandService.setPKClass(Long.class);
			tempBrandItems = new SelectItem[theBrandCodes.size()];
			for (int i=0; i< theBrandCodes.size(); i ++) {
				String brandCode = theBrandCodes.get(i);
				Brand brand = brandService.findUniqueBy("brandCode", brandCode);
				tempBrandItems[i] = new SelectItem(brand.getBrandCode(),brand.getBrandName());
			}
		} else {
			tempBrandItems = new SelectItem[1];
			tempBrandItems[0] = new SelectItem("","");
		}
		return tempBrandItems;
		// end set brand code items
	}
	protected SelectItem[] initSecondCategory(){
		IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
		goodsService.setEntityClass(Goods.class);
		goodsService.setPKClass(Long.class);
		// start set secondCategory items
		List<String> params = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		generatePropertyParamsAndValues(EjdConstants.SECOND_CATEGORY_CODE, params, values);
		List<String> theSecondCategories = goodsService.getPropertyCodes(EjdConstants.SECOND_CATEGORY_CODE,params,values);
		SelectItem[] tempSecondCategoryItems = null;
		if (null != theSecondCategories && !(theSecondCategories.isEmpty())) {
			ISubCategoryService<SubCategory,Long> subCategoryService = (ISubCategoryService<SubCategory,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.SUB_CATEGORY_SERVICE_BEAN_NAME);
			subCategoryService.setEntityClass(SubCategory.class);
			subCategoryService.setPKClass(Long.class);
			tempSecondCategoryItems = new SelectItem[theSecondCategories.size()];
			for (int i=0; i< theSecondCategories.size(); i ++) {
				String subCategoryCode = theSecondCategories.get(i);
				SubCategory subCategory = subCategoryService.findUniqueBy("subCategoryCode", subCategoryCode);
				String subCategoryNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,subCategory.getSubCategoryName(), FacesMessage.SEVERITY_ERROR).getDetail();
				tempSecondCategoryItems[i] = new SelectItem(subCategory.getSubCategoryCode(),subCategoryNameN18,subCategory.getCategoryCode());
				
			}
		} else {
			tempSecondCategoryItems = new SelectItem[1];
			tempSecondCategoryItems[0] = new SelectItem("","");
		}
		return tempSecondCategoryItems;
		// end set secondCategory items
	}
	protected SelectItem[] initMaterial(){
		IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
		goodsService.setEntityClass(Goods.class);
		goodsService.setPKClass(Long.class);
		// start set material code items
		List<String> params = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		generatePropertyParamsAndValues(EjdConstants.MATERIAL, params, values);
		List<String> theMaterials = goodsService.getPropertyCodes(EjdConstants.MATERIAL,params, values);
		SelectItem[] tempMaterialItems = null;
		if (null != theMaterials && !(theMaterials.isEmpty())) {
			IMaterialService<Material,Long> materialService = (IMaterialService<Material,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.MATERIAL_SERVICE_BEAN_NAME);
			materialService.setEntityClass(Material.class);
			materialService.setPKClass(Long.class);
			tempMaterialItems = new SelectItem[theMaterials.size()];
			for (int i=0; i< theMaterials.size(); i ++) {
				String materialCode = theMaterials.get(i);
				Material material = materialService.findUniqueBy("materialCode", materialCode);
				String materialNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,material.getMaterialName(), FacesMessage.SEVERITY_ERROR).getDetail();
				tempMaterialItems[i] = new SelectItem(material.getMaterialCode(),materialNameN18);
			}
		} else {
			tempMaterialItems =  new SelectItem[1];
			tempMaterialItems[0] = new SelectItem("","");
		}
		return tempMaterialItems;
		// end set material code items
	}
	protected SelectItem[] initColor(){
		IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
		goodsService.setEntityClass(Goods.class);
		goodsService.setPKClass(Long.class);
		List<String> params = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		generatePropertyParamsAndValues(EjdConstants.COLOR, params, values);
		// start set color code items
		List<String> theColors = goodsService.getPropertyCodes(EjdConstants.COLOR,params, values);
		SelectItem[] tempColorItems = null;
		if (null != theColors && !(theColors.isEmpty())) {
			IColorService<Color,Long> colorService = (IColorService<Color,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.COLOR_SERVICE_BEAN_NAME);
			colorService.setEntityClass(Color.class);
			colorService.setPKClass(Long.class);
			tempColorItems = new SelectItem[theColors.size()];
			for (int i=0; i< theColors.size(); i ++) {
				String colorId = theColors.get(i);
				Color color = colorService.findUniqueBy("colorId", colorId);
				String colorNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,color.getColorName(), FacesMessage.SEVERITY_ERROR).getDetail();
				tempColorItems[i] = new SelectItem(color.getColorId(), colorNameN18);
			}
		} else {
			tempColorItems = new SelectItem[1];
			tempColorItems[0] = new SelectItem("","");
		}
		return tempColorItems;
		// end set color code items
	}
	
	
	
	protected void generatePropertyParamsAndValues(String currentField, List<String> params, List<String> values){
		if (null == params) {
			params = new ArrayList<String>();
		}
		params.clear();
		if (null == values) {
			values = new ArrayList<String>();
		}
		values.clear();
		//start set primaryCategoryCode
		params.add(EjdConstants.PRIMARY_CATEGORY_CODE);
		values.add(EjdConstants.PRIMARY_CATEGORY_CODE_BAGS);
		//end set primaryCategoryCode
		// set apply start
		if (null == this.getApplys()) {
			this.setApplys(new String[]{""});
		}
		if (!(EjdConstants.APPLY.equals(currentField))) {
			if (!"".equals(this.getApplys()[0])) {
				params.add(EjdConstants.APPLY);
				values.add(this.getApplys()[0]);
			}
		}
		// set apply end
		// set brandCode start
		if (null == this.getBrandCodes()) {
			this.setBrandCodes(new String[]{""});
		}
		if (!(EjdConstants.BRAND_CODE.equals(currentField))) {
			if (!"".equals(this.getBrandCodes()[0])) {
				params.add(EjdConstants.BRAND_CODE);
				values.add(this.getBrandCodes()[0]);
			}
		}
		// set brandCode end
		// set SecondCategories start
		if (null == this.getSecondCategories()) {
			this.setSecondCategories(new String[]{""});
		}
		if (!(EjdConstants.SECOND_CATEGORY_CODE.equals(currentField))) {
			if (!"".equals(this.getSecondCategories()[0])) {
				params.add(EjdConstants.SECOND_CATEGORY_CODE);
				values.add(this.getSecondCategories()[0]);
			}
		}
		// set SecondCategories end
		// set material start
		if (null == this.getMaterials()) {
			this.setMaterials(new String[]{""});
		}
		if (!(EjdConstants.MATERIAL.equals(currentField))) {
			if (!"".equals(this.getMaterials()[0])) {
				params.add(EjdConstants.MATERIAL);
				values.add(this.getMaterials()[0]);
			}
		}
		// set material end
		// set color start
		if (null == this.getColors()) {
			this.setColors(new String[]{""});
		}
		if (!(EjdConstants.COLOR.equals(currentField))) {
			if (!"".equals(this.getColors()[0])) {
				params.add(EjdConstants.COLOR);
				values.add(this.getColors()[0]);
			}
		}
		// set color end
		
		
		
	}
	
	// this function for after select one property to ini again
	protected void reInitAllProperties(){
		SelectItem[] currentApplyItems = this.initApply();
		CommonUtil.refreshItems(this.getApplyItems(),currentApplyItems);
		SelectItem[] currentBrandCodeItems = this.initBrandCodes();
		CommonUtil.refreshItems(this.getBrandCodeItems(),currentBrandCodeItems);
		SelectItem[] currentSecondCategoryItems = this.initSecondCategory();
		CommonUtil.refreshItems(this.getSecondCategoryItems(),currentSecondCategoryItems);
		SelectItem[] currentMaterialItems = this.initMaterial();
		CommonUtil.refreshItems(this.getMaterialItems(),currentMaterialItems);
		SelectItem[] currentColorItems = this.initColor();
		CommonUtil.refreshItems(this.getColorItems(),currentColorItems);
	}
	//end
	
	public String goDetailPage(){
		String primaryCategoryCode = (String) SpringFacesUtil.getRequestParameter("primaryCategoryCode");
		String code =(String) SpringFacesUtil.getRequestParameter("code");
		String productId =(String) SpringFacesUtil.getRequestParameter("productId");
		if (PageExistUtil.checkPageExist(code+".xhtml", "/layout/goods/shoes") && PageExistUtil.checkPageExist(code+"Detail.xhtml", "/layout/goods/shoes/sub") ) {
			return code + "?faces-redirect=true&primaryCategoryCode=" + DESPlusUtil.encrypt(primaryCategoryCode) + "&code=" + DESPlusUtil.encrypt(code) + "&productId=" + DESPlusUtil.encrypt(productId);
		} else {
			return "/WEB-INF/" + EjdConstants.NO_PRODUCT_DETAIL_PAGE;
		}
	}
	// this function is generate hql and parameters.
	protected void generateHQL(String defaultHql) {//defaultHql is default hql, values is parameter
		String hql = defaultHql;
		List paraList = new ArrayList();
		if (null != this.getApplys() && this.getApplys().length > 0 && (null != this.getApplys()[0]) && (!("".equals(this.getApplys()[0])))) {
			hql = hql + " and sfield1 in (";
			String[] tempApplys = this.getApplys();
			for (int i=0 ; i < tempApplys.length; i++) {
				hql = hql + "?";
				if (i != (tempApplys.length -1)) {
					hql = hql + ",";
				}
				paraList.add(tempApplys[i]);
			}
			hql = hql + ") ";
		}
		if (null != this.getBrandCodes() && this.getBrandCodes().length > 0 && (null != this.getBrandCodes()[0]) && (!("".equals(this.getBrandCodes()[0])))) {
			hql = hql + " and brandCode in (";
			String[] tempBrandCodes = this.getBrandCodes();
			for (int i=0 ; i < tempBrandCodes.length; i++) {
				hql = hql + "?";
				if (i != (tempBrandCodes.length -1)) {
					hql = hql + ",";
				}
				paraList.add(tempBrandCodes[i]);
			}
			hql = hql + ") ";
		}
		if (null != this.getSecondCategories() && this.getSecondCategories().length > 0 && (null != this.getSecondCategories()[0]) && (!("".equals(this.getSecondCategories()[0])))) {
			hql =  hql + " and secondCategoryCode in (";
			String[] tempSecondCategories = this.getSecondCategories();
			for (int i=0 ; i < tempSecondCategories.length; i++) {
				hql = hql + "?";
				if (i != (tempSecondCategories.length -1)) {
					hql = hql + ",";
				}
				paraList.add(tempSecondCategories[i]);
			}
			hql = hql + ") ";
		}
		
		if (null != this.getMaterials() && this.getMaterials().length > 0 && (null != this.getMaterials()[0]) && (!("".equals(this.getMaterials()[0])))) {
			hql =  hql + " and sfield2 in (";
			String[] tempMaterials = this.getMaterials();
			for (int i=0 ; i < tempMaterials.length; i++) {
				hql = hql + "?";
				if (i != (tempMaterials.length -1)) {
					hql = hql + ",";
				}
				paraList.add(tempMaterials[i]);
			}
			hql = hql + ") ";
		}
		if (null != this.getColors() && this.getColors().length > 0 && (null != this.getColors()[0]) && (!("".equals(this.getColors()[0])))) {
			hql =  hql + " and sfield3 in (";
			String[] tempColors = this.getColors();
			for (int i=0 ; i < tempColors.length; i++) {
				hql = hql + "?";
				if (i != (tempColors.length -1)) {
					hql = hql + ",";
				}
				paraList.add(tempColors[i]);
			}
			hql = hql + ") ";
		}
		
		if (null != this.getPriceBetween() && !("N".equals(this.getPriceBetween()))) {
			if ("A".equals(this.getPriceBetween())) {
				hql = hql + " and retailPrice < 200 " ;
			}
			if ("B".equals(this.getPriceBetween())) {
				hql = hql + " and retailPrice >= 200 and retailPrice < 300 " ;
			}
			if ("C".equals(this.getPriceBetween())) {
				hql = hql + " and retailPrice >=300  and retailPrice < 400 " ;
			}
			if ("D".equals(this.getPriceBetween())) {
				hql = hql + " and retailPrice >=400  and retailPrice < 500 " ;
			}
			if ("E".equals(this.getPriceBetween())) {
				hql = hql + " and retailPrice >= 500 " ;
			}
		}
		if (null == this.getSortStyle() || "NO".equals(this.getSortStyle())) {
			hql = hql + " order by viewInt asc ";
		} else if ("PA".equals(this.getSortStyle())) {
			hql = hql + " order by retailPrice asc ";
		} else if ("PD".equals(this.getSortStyle())) {
			hql = hql + " order by retailPrice desc ";
		} else if ("TA".equals(this.getSortStyle())) {
			hql = hql + " order by timeToMarket asc ";
		} else if ("TD".equals(this.getSortStyle())) {
			hql = hql + " order by timeToMarket desc ";
		}
		Object[] tempValues = null;
		if (paraList.size() > 0) {
			tempValues = new Object[paraList.size()];
			for (int i = 0; i< tempValues.length;i++) {
				tempValues[i] = paraList.get(i);
			}
		}
		this.setValues(tempValues);
		this.setHql(hql);
	}
	protected void getPageDatas() {
		Page<Goods,Long> resultPage = this.getData(this.getHql(), this.getPageNo(), this.getPageSize(), this.getValues());
		this.setPage(resultPage);
		SelectItem[] tempPageItem = new SelectItem[(int)resultPage.getTotalPageCount()];
		for (int j=0;j<tempPageItem.length;j++) {
			tempPageItem[j]= new SelectItem(new Integer(j+1),String.valueOf(j+1));
		}
		this.setPageItems(tempPageItem);
	}
	public String changePageNo(){
		System.out.println(this.getPageNo());
		this.getPageDatas();
		return null;
	}
	public String selectPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.setPageNo(Integer.valueOf(pageNo).intValue());
		this.getPageDatas();
		return result;
		
	}
	public void pageSizeValueChangeListener(ValueChangeEvent event) {
		Integer newPageSize = (Integer)event.getNewValue();
		this.setPageSize(newPageSize);
		this.setPageNo(1);
		this.getPageDatas();
	}
	public void searchParamChangeListener(ValueChangeEvent event){
		this.setSearchParamChangeFlag(true);
		System.out.println("======="+"true");
	}
	
	// start used for men and women common function
	public String searchDataNoneParamByCustomer() {
		this.setPageNo(1);
		if (!("N".equals(this.getPriceBetween()))) {
			this.setPriceBetween("N");
		}
		if (null != this.getBrandCodes()) {
			this.getBrandCodes()[0] = "";
		}
		if (null != this.getSecondCategories()) {
			this.getSecondCategories()[0] = "";
		}
		if (null != this.getMaterials()) {
			this.getMaterials()[0] = "";
		}
		if (null != this.getColors()) {
			this.getColors()[0] = "";
		}
		
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		return null;
	}
	public void priceBetweenValueChangeListener(ValueChangeEvent event) {
		String priceBetween = (String)event.getNewValue();
		this.setPageNo(1);
		this.setPriceBetween(priceBetween);
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
	}
	public void sortStyleValueChangeListener(ValueChangeEvent event) {
		String sortStyle = (String)event.getNewValue();
		this.setPageNo(1);
		this.setSortStyle(sortStyle);
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
	}
	
	
	public String selectApply(){
		String apply = (String) SpringFacesUtil.getRequestParameter("apply");
		if (null != this.getApplys()) {
			if (this.getApplys().length > 0 && (this.getApplys()[0].equals(apply))) {
				this.getApplys()[0] = "";
			} else {
				this.getApplys()[0] = apply;
			}
		} else {
			String[] tempApplys = new String[1];
			tempApplys[0] = apply;
			this.setApplys(tempApplys);
		}
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		reInitAllProperties();
		return null;
	}
	public String unSelectApply(){
		if (null != this.getApplys()) {
			this.setApplys(new String[]{""});
		} else {
			this.getApplys()[0] = "";
		}
		reInitAllProperties();
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		return null;
	}
	public String selectBrandCode(){
		String brandCode = (String) SpringFacesUtil.getRequestParameter("brandCode");
		if (null != this.getBrandCodes()) {
			if (this.getBrandCodes().length > 0 && (this.getBrandCodes()[0].equals(brandCode))) {
				this.getBrandCodes()[0] = "";
			} else {
				this.getBrandCodes()[0] = brandCode;
			}
		} else {
			String[] tempBrandCodes = new String[1];
			tempBrandCodes[0] = brandCode;
			this.setBrandCodes(tempBrandCodes);
		}
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		reInitAllProperties();
		return null;
	}
	public String unSelectBrandCode(){
		if (null != this.getBrandCodes()) {
			this.setBrandCodes(new String[]{""});
		} else {
			this.getBrandCodes()[0] = "";
		}
		reInitAllProperties();
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		return null;
	}
	public String selectSecondCategory(){
		String secondCategory = (String) SpringFacesUtil.getRequestParameter("secondCategory");
		if (null != this.getSecondCategories()) {
			if (this.getSecondCategories().length > 0 && (this.getSecondCategories()[0].equals(secondCategory))) {
				this.getSecondCategories()[0] = "";
			} else {
				this.getSecondCategories()[0] = secondCategory;
			}
		} else {
			String[] tempSecondCategory = new String[1];
			tempSecondCategory[0] = secondCategory;
			this.setSecondCategories(tempSecondCategory);
		}
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		reInitAllProperties();
		return null;
	}
	public String unSelectSecondCategory(){
		if (null != this.getSecondCategories()) {
			this.setSecondCategories(new String[]{""});
		} else {
			this.getSecondCategories()[0] = "";
		}
		reInitAllProperties();
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		return null;
	}
	public String selectMaterial(){
		String material = (String) SpringFacesUtil.getRequestParameter("material");
		if (null != this.getMaterials()) {
			if (this.getMaterials().length > 0 && (this.getMaterials()[0].equals(material))) {
				this.getMaterials()[0] = "";
			} else {
				this.getMaterials()[0] = material;
			}
		} else {
			String[] tempMaterials = new String[1];
			tempMaterials[0] = material;
			this.setMaterials(tempMaterials);
		}
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		reInitAllProperties();
		return null;
	}
	public String unSelectMaterial(){
		if (null != this.getMaterials()) {
			this.setMaterials(new String[]{""});
		} else {
			this.getMaterials()[0] = "";
		}
		reInitAllProperties();
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		return null;
	}
	public String selectColor(){
		String color = (String) SpringFacesUtil.getRequestParameter("color");
		if (null != this.getColors()) {
			if (this.getColors().length > 0 && (this.getColors()[0].equals(color))) {
				this.getColors()[0] = "";
			} else {
				this.getColors()[0] = color;
			}
		} else {
			String[] tempColors = new String[1];
			tempColors[0] = color;
			this.setColors(tempColors);
		}
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		reInitAllProperties();
		return null;
	}
	public String unSelectColor(){
		if (null != this.getColors()) {
			this.setColors(new String[]{""});
		} else {
			this.getColors()[0] = "";
		}
		reInitAllProperties();
		this.generateHQL(this.getDefaultHql());
		this.getPageDatas();
		return null;
	}
	
	// start used for men and women common function
	public List<SelectItem> getSelectProperties(){
		List<SelectItem> result = new ArrayList<SelectItem>();
		if (null != this.getApplys() && this.getApplys().length > 0 && (null != this.getApplys()[0]) && (!("".equals(this.getApplys()[0])))) {
			
		}
		if (null != this.getBrandCodes() && this.getBrandCodes().length > 0 && (null != this.getBrandCodes()[0]) && (!("".equals(this.getBrandCodes()[0])))) {
			
		}
		if (null != this.getSecondCategories() && this.getSecondCategories().length > 0 && (null != this.getSecondCategories()[0]) && (!("".equals(this.getSecondCategories()[0])))) {
			
		}
		
		if (null != this.getMaterials() && this.getMaterials().length > 0 && (null != this.getMaterials()[0]) && (!("".equals(this.getMaterials()[0])))) {
			
		}
		if (null != this.getColors() && this.getColors().length > 0 && (null != this.getColors()[0]) && (!("".equals(this.getColors()[0])))) {
			
		}
		
		
		return result;
	}
	public String getApplySelectedLabel() {
		String result ="";
		if (null != this.getApplys() && !("".equals(this.getApplys()[0]))) {
			String tempApply = this.getApplys()[0];
			SelectItem[] tempApplyItems = this.getApplyItems();
			for(int i = 0 ; i < tempApplyItems.length; i ++) {
				SelectItem item = tempApplyItems[i];
				if (tempApply.equals(item.getValue())) {
					result = item.getLabel();
					break;
				}
			}
		}
		return result;
	}
	public String getBrandCodeSelectedLabel() {
		String result ="";
		if (null != this.getBrandCodes() && !("".equals(this.getBrandCodes()[0]))) {
			String tempBrandCode = this.getBrandCodes()[0];
			SelectItem[] tempBrandCodeItems = this.getBrandCodeItems();
			for(int i = 0 ; i < tempBrandCodeItems.length; i ++) {
				SelectItem item = tempBrandCodeItems[i];
				if (tempBrandCode.equals(item.getValue())) {
					result = item.getLabel();
					break;
				}
			}
		}
		return result;
	}
	public String getSecondCategorySelectedLabel() {
		String result ="";
		if (null != this.getSecondCategories() && !("".equals(this.getSecondCategories()[0]))) {
			String tempSecondCategory = this.getSecondCategories()[0];
			SelectItem[] tempSecondCategoryItems = this.getSecondCategoryItems();
			for(int i = 0 ; i < tempSecondCategoryItems.length; i ++) {
				SelectItem item = tempSecondCategoryItems[i];
				if (tempSecondCategory.equals(item.getValue())) {
					result = item.getLabel();
					break;
				}
			}
		}
		return result;
	}
	public String getMaterialSelectedLabel() {
		String result ="";
		if (null != this.getMaterials() && !("".equals(this.getMaterials()[0]))) {
			String tempMaterial = this.getMaterials()[0];
			SelectItem[] tempMaterialItems = this.getMaterialItems();
			for(int i = 0 ; i < tempMaterialItems.length; i ++) {
				SelectItem item = tempMaterialItems[i];
				if (tempMaterial.equals(item.getValue())) {
					result = item.getLabel();
					break;
				}
			}
		}
		return result;
	}
	
	public String getColorSelectedLabel() {
		String result ="";
		if (null != this.getColors() && !("".equals(this.getColors()[0]))) {
			String tempColor = this.getColors()[0];
			SelectItem[] tempColorItems = this.getColorItems();
			for(int i = 0 ; i < tempColorItems.length; i ++) {
				SelectItem item = tempColorItems[i];
				if (tempColor.equals(item.getValue())) {
					result = item.getLabel();
					break;
				}
			}
		}
		return result;
	}
}
